package business.global.pk.ddz;

import business.ddz.c2s.cclass.DDZRoom_PosEnd;
import business.global.room.base.AbsBaseRoom;
import business.global.room.base.AbsRoomPos;
import cenum.PrizeType;
import cenum.RoomTypeEnum;
import com.ddm.server.common.utils.CommMath;
import jsproto.c2s.cclass.pk.BasePockerLogic;
import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 资阳大局位置
 */
public class DDZRoomPos extends AbsRoomPos {

    public ArrayList<Integer> privateCards = new ArrayList<>(); // 手牌
    private int m_nWin = 0; // 赢场数
    private int m_nLose = 0; // 输场数
    private int m_nFlat = 0; // 平场数
    private	int isOpenCardStartGame = -1;//-1未表态,0不明牌开始1：明牌开始
    private Boolean beginFlag=false;//开始标识
    private	ArrayList<ArrayList<Integer>> publicCardList = new ArrayList<>();//打出的牌
    private int tuoGuanSetCount = 0; // 连续托管局数

    public DDZRoomPos(int posID, AbsBaseRoom room) {
        super(posID, room);
    }

    /**
     * 初始化手牌init
     *
     * @param cards
     */
    public void init(List<Integer> cards) {
        this.privateCards = new ArrayList<>(cards);
        this.publicCardList.clear();
    }

    /**
     * 获取牌组信息
     *
     * @param pid 玩家id
     * @return
     */
    public ArrayList<Integer> getNotifyCard(long pid, boolean isOpenCard) {
        boolean isSelf = pid == this.getPid();
        if (isOpenCard || this.getRoom().getBaseRoomConfigure().getPrizeType() == PrizeType.Gold) {
            isSelf = true;
        }
        ArrayList<Integer> posList = new ArrayList<>();
        // 是自己
        int length = privateCards.size();
        for (int i = 0; i < length; i++) {
            posList.add(isSelf ? privateCards.get(i) : 0);
        }
        return posList;
    }

    /**
     * 删除牌组信息,这里删除了，不管失败成功，privateCards都会改变
     *
     * @param cardList
     * @return
     */
    public boolean deleteCard(ArrayList<Integer> cardList) {
        long count = cardList.stream().distinct().count();
        if (count == cardList.size() && this.privateCards.containsAll(cardList)) {
            return privateCards.removeAll(cardList);
        }
        return false;
    }

    /**
     * 判断玩家手牌是否拥有该牌,花色和牌值都相等的
     *
     * @param card 指定牌
     * @return
     */
    public boolean checkCard(Integer card) {
        return BasePockerLogic.getCardCount(privateCards, card, false) > 0;
    }

    /**
     * 大局结算
     *
     * @return
     */
    public DDZRoom_PosEnd calcPosEnd(DDZSetPos setPos) {
        DDZRoomSet set = (DDZRoomSet) this.getRoom().getCurSet();
        ArrayList<Integer> beiShuList = set.getDoubleNumList();
        DDZRoom_PosEnd posEnd = new DDZRoom_PosEnd();
        int setPoint = set.pointList.get(this.getPosID());
        posEnd.point = setPoint;
        posEnd.pos = this.getPosID();
        posEnd.pid = this.getPid();
        if (set.room.calcFenUseYiKao() && RoomTypeEnum.UNION.equals(this.getRoom().getRoomTypeEnum())) {
            posEnd.sportsPoint = setPos.getDeductPointYiKao();
        } else {
            posEnd.sportsPoint = setSportsPoint(setPoint);
        }
        posEnd.baseMark = this.getRoom().getBaseMark();
        posEnd.isLandowner = set.landowner == this.getPosID();
        posEnd.robClose = set.robCloseList.get(this.getPosID());
        posEnd.doubleNum = beiShuList.get(this.getPosID());
        this.calcRoomPoint(set.pointList.get(this.getPosID()));
        return posEnd;
    }

    /**
     * 计算房间分数
     */
    @Override
    public void calcRoomPoint(int point) {
        DDZRoomSet set = (DDZRoomSet) this.getRoom().getCurSet();
        if(set==null|| MapUtils.isEmpty(set.getPosDict())){
            // todo 防止当前局出现空（例如吊蟹在牌局开始之前需扣底分）
            super.calcRoomPoint(point);
            return;
        }
        DDZSetPos setPos = set.getPosDict().get(this.getPosID());
        this.setPoint(this.getPoint() + point);
        this.setPointYiKao(CommMath.addDouble(this.getPointYiKao() ,setPos.getDeductEndPoint()));
        this.calcRoomSportsPoint(point,setPos.getDeductEndPoint());
        if (getRoom().isRulesOfCanNotBelowZero() && RoomTypeEnum.UNION.equals(this.getRoom().getRoomTypeEnum())) {
            //小于0的 直接重置0
            if (this.getRoomSportsPoint() <= 0) {
                this.setRoomSportsPoint(0D);
            }
        }
    }

    /**
     * 竞技点
     */
    @Override
    public Double sportsPoint() {
        if ((getRoom().calcFenUseYiKao()||getRoom().isRulesOfCanNotBelowZero()) && RoomTypeEnum.UNION.equals(this.getRoom().getRoomTypeEnum())) {
            if (RoomTypeEnum.UNION.equals(this.getRoom().getRoomTypeEnum())) {
                return CommMath.FormatDouble(this.getPointYiKao());
            } else {
                return null;
            }
        }
        return super.sportsPoint();
    }

    public ArrayList<Integer> getPrivateCards() {
        return privateCards;
    }

    public int getWin() {
        return m_nWin;
    }

    public void addWin(int nWin) {
        this.m_nWin += nWin;
    }

    public int getLose() {
        return m_nLose;
    }

    public void addLose(int nLose) {
        this.m_nLose += nLose;
    }

    /**
     *
     */
    public void addFlat(int nFlat) {
        this.m_nFlat += nFlat;
    }

    public int getFlat() {
        return m_nFlat;
    }

    /**
     * 添加手牌
     * @param cards
     */
    public void addAllCard(List<Integer> cards) {
        this.privateCards.addAll(cards);
    }

    public int getOpenCardStartGame() {
        return isOpenCardStartGame;
    }

    public void setOpenCardStartGame(int openCardStartGame) {
        isOpenCardStartGame = openCardStartGame;
    }

    public Boolean getBeginFlag() {
        return beginFlag;
    }

    public void setBeginFlag(Boolean beginFlag) {
        this.beginFlag = beginFlag;
    }

    /**
     *添加打出的牌
     * */
    public void addPublicCard(ArrayList<Integer> cardList) {
        if(null == cardList || (null == cardList&&  cardList.size() <= 0)) {
            return;
        }
        this.publicCardList.add(cardList);
    }

    public ArrayList<ArrayList<Integer>> getPublicCardList() {
        return publicCardList;
    }

    public int getTuoGuanSetCount() {
        return tuoGuanSetCount;
    }

    public void setTuoGuanSetCount(int tuoGuanSetCount) {
        this.tuoGuanSetCount = tuoGuanSetCount;
    }

    /**
     * 增加托管局数
     */
    public void addTuoGuanSetCount(){
        tuoGuanSetCount += 1;
    }

    /**
     * 连续托管局数清零
     */
    public void clearTuoGuanSetCount(){
        tuoGuanSetCount = 0;
    }

    /**
     * 设置托管状态
     *
     * @param isTrusteeship 托管状态
     * @param isOwn         是否屏蔽自己
     */
    public void setTrusteeship(boolean isTrusteeship, boolean isOwn) {
        if (this.isTrusteeship() == isTrusteeship) {
            return;
        }
        // 托管2小局解散：连续2局托管
        if(!isTrusteeship){ // 玩家取消托管，连续托管局数清零
            this.clearTuoGuanSetCount();
        }
        this.setTrusteeship(isTrusteeship);
        if (isOwn) {
            this.getRoom().getRoomPosMgr().notify2ExcludePosID(this.getPosID(), this.getRoom().Trusteeship(this.getRoom().getRoomID(), this.getPid(), this.getPosID(), this.isTrusteeship()));
        } else {
            this.getRoom().getRoomPosMgr().notify2All(this.getRoom().Trusteeship(this.getRoom().getRoomID(), this.getPid(), this.getPosID(), this.isTrusteeship()));
        }
    }
}
